<?php
session_start();

// 初始化游戏数据
if (!isset($_SESSION['game'])) {
    $_SESSION['game'] = [
        'players' => ['狼人', '村民', '预言家', '女巫'],
        'alive' => [true, true, true, true],
        'phase' => '夜晚',
        'current_action' => '狼人行动'
    ];
}

// 处理行动逻辑
if (isset($_GET['action'])) {
    $action = $_GET['action'];
    $game = &$_SESSION['game'];

    switch ($action) {
        case 'wolf_kill':
            $target = $_GET['target'];
            $game['alive'][$target] = false;
            $game['current_action'] = '预言家行动';
            break;
        case 'seer_check':
            $target = $_GET['target'];
            $role = $game['players'][$target];
            $result = ($role === '狼人') ? '是狼人' : '不是狼人';
            $game['current_action'] = '女巫行动';
            break;
        case 'witch_heal':
            $game['current_action'] = '白天投票';
            $game['phase'] = '白天';
            break;
        case 'witch_poison':
            $target = $_GET['target'];
            $game['alive'][$target] = false;
            $game['current_action'] = '白天投票';
            $game['phase'] = '白天';
            break;
        case 'vote':
            $target = $_GET['target'];
            $game['alive'][$target] = false;
            $game['phase'] = '夜晚';
            $game['current_action'] = '狼人行动';
            break;
    }
}

// 检查游戏是否结束
function isGameOver($game) {
    $wolfCount = 0;
    $villagerCount = 0;
    foreach ($game['players'] as $index => $role) {
        if ($game['alive'][$index]) {
            if ($role === '狼人') {
                $wolfCount++;
            } else {
                $villagerCount++;
            }
        }
    }
    return $wolfCount === 0 || $villagerCount === 0;
}

$game = $_SESSION['game'];
$gameOver = isGameOver($game);
?>